function [optimo,centroide_optimo]=repeat_kmeans(k, n, data)
%load iris.dat

indices = cell(n, 1);  % a qué centroide pertenece cada dato
centroides = cell(n, 1);   % lista de centroides


for j = 1:n
    [bmus, kms] = kmeans(data,k,'Distance','sqEuclidean','emptyaction','singleton');
    
    indices{j} = bmus; 
    centroides{j} = kms;

end

c = zeros(n,1);
for j = 1:n
    for i = 1:k
        temp = find(i == indices{j});
        
        suma = 0;
        suma_total = 0;
        for t = length(temp)
            suma = suma + pdist([centroides{j}(i,:); data(temp(t),:)]);
        end
        suma_total = suma_total + (suma / length(temp));
    end
    c(j) = suma_total / k;
end

[zz, z] = min (c);

strcat('El menor coeficiente interno de dispersión promedio es: ',num2str(zz))
strcat('El peor coeficiente interno de dispersión promedio es: ',num2str(max(c)))

optimo = indices{z};
centroide_optimo = centroides{z};

%    for m = 1 : k
%        res = find(k == bmus) 
